package com.artfii;

import com.artfii.hibernate.HibernateImpl;
import com.artfii.hibernate.HibernateShardingInterceptor;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Repository
public class TestShardingDao extends HibernateImpl<TestSharding, Long> {

    private HibernateShardingInterceptor interceptor = HibernateShardingInterceptor.ofYearSharding("test_sharding");

    @Resource
    public void setSessionFactory(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }

    /**
     * 加入了分表保存
     * @param entity
     * @return
     */
    public Long save(TestSharding entity) {
        Long pk = super.save(entity);
        //使用拦截器分表的方式：下面拦截后保存到分表
        interceptor.save(sessionFactory, entity,entity.getCreateDate());
        return pk;
    }

    public List findListOfStartDate(Date startDate) {
        String hql = " from TestSharding where createDate>=?";
        List<TestSharding> testShardingList = list(hql, new Object[]{startDate});
        return testShardingList;
    }

    public List findListOfEndDate(Date end) {
        String hql = " from TestSharding where createDate <= ?";
        List<TestSharding> testShardingList = list(hql, new Object[]{end});
        return testShardingList;
    }

    public List findListOfBothDate(Date start, Date end) {
        String hql = " from TestSharding where createDate >= ? and createDate <= ?";
        List<TestSharding> testShardingList = list(hql, new Object[]{start, end});
        return testShardingList;
    }

    public List findListOfBetweenDate(Date start, Date end) {
        String hql = " from TestSharding where createDate between ? and ? ";
        List<TestSharding> testShardingList = list(hql, new Object[]{start, end});
        return testShardingList;
    }


}